package camera.mediarecorder;

import android.annotation.TargetApi;
import android.media.AudioRecord;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.os.Build;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;

import java.io.IOException;

import camera.OpenGl.InputSurface;
import camera.mediarecorder.configuration.AudioConfiguration;
import camera.mediarecorder.configuration.AudioMediaCodec;
import camera.mediarecorder.configuration.VideoConfiguration;
import camera.mediarecorder.configuration.VideoMediaCodec;

/**
 *@author:ike
 *@function: 
 *@creat time:11:29 2018/1/3
 **/

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public class CameraRecorder {
    private String Tag=CameraRecorder.class.getSimpleName();
    private MediaMuxer mMediaMuxer;
    private AudioRecord mAudioRecord;
    private int videoTrack;
    private int audioTrack;
    
    private AudioConfiguration audioConfiguration;
    private VideoConfiguration videoConfiguration;
    private String savePath;
    
    private MediaCodec audioCodec;
    private MediaCodec videoCodec;
    private MediaCodec.BufferInfo bufferInfo=new MediaCodec.BufferInfo();
    //EGL环境
    private InputSurface mInputSuface;
   

    private boolean isFirsSetup=true;
    
    public CameraRecorder videoSavePath(String savePath){
        this.savePath=savePath;
        return this;
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public CameraRecorder prepare(){
        //配置音频
        audioCodec = AudioMediaCodec.getAudioMediaCodec(audioConfiguration);
        //配置视频
        videoCodec= VideoMediaCodec.getVideoMediaCodec(videoConfiguration);
        //配置混合器
        try {
            mMediaMuxer=new MediaMuxer(savePath, MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }
    
    public void encordVideo(){
        int outputBufferIndex = videoCodec.dequeueOutputBuffer(bufferInfo, 10000);
        while (isStarted){
            
        }
    }


    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    public boolean isFirstSetUp() {
        if (videoCodec==null||mInputSuface!=null){
            return false;
        }
       if (!isFirsSetup){
            return false;
       }
       mInputSuface=new InputSurface(videoCodec.createInputSurface());
       videoCodec.start();
       isStarted=true;
       isFirsSetup=false;
       return true;
    }
    
    ///////////////////////////////////////////////////////外部方法
    public boolean isStarted() {
        return isStarted;
    }

    public void setStarted(boolean started) {
        isStarted = started;
    }

    private boolean isStarted;

    public boolean isFirsSetup() {
        return isFirsSetup;
    }

    public void setFirsSetup(boolean firsSetup) {
        isFirsSetup = firsSetup;
    }
}
